
<!DOCTYPE html>

<html lang="zh_CN">
<head>
    <#include "../common/header.ftl"/>
    <link rel="stylesheet" href="/js/plugins/treeview/bootstrap-treeview.min.css" type="text/css" />
    <script type="text/javascript" src="/js/plugins/treeview/bootstrap-treeview.min.js"></script>
    <script>
        $(function () {
            //打开页面获取根地区数据
            $.get(
                "/region/selectByParentId.do",
                {type:'tree'},
                function (data) {
                    var defaultDate=[
                        {
                            text:'全部地区',
                            nodes:data
                        }
                    ];


                    $('#treeview').treeview({
                        data: defaultDate,
                        showTags:true,
                        lazyLoad:function (node) {//第一个参数是该节点，第二个参数是加载数据的函数
                            $.get('/region/selectByParentId.do',{parentId:node.id,type:'tree'},function (data) {
                                $('#treeview').treeview('addNode', [ data, node]);
                            });
                        },
                        onNodeSelected: function(event, node) {
                            //回显父地区信息
                            // $("#parentId").val(node.id);
                            // $("#parentName").val(node.text);
                            $.get('/region/selectByParentId.do',{parentId:node.id},function (data) {
                                /*var temp = "";
                                $.each(data, function (index, ele) {
                                    temp += "<tr><td>" + (index + 1) + "</td><td>" + ele.name + "</td></tr>"
                                });
                                $(".regionTable tbody").html(temp);*/
                                //先清空
                                $(".regionTable tbody").empty();
                                $.each(data, function (index, ele) {
                                    var tr=$("#template tr").clone(true);
                                    $(tr).find("td:nth-child(1)").html(index + 1);
                                    $(tr).find("td:nth-child(2)").html(ele.name);
                                    $(tr).find("a").attr("data-json", ele.json);
                                    if(ele.state) {
                                        $(tr).find("a:nth-child(2) span").html("取消推荐");
                                    }
                                $(".regionTable tbody").append(tr);
                                });
                            });
                        }
                    });
                }
            );

            //添加/编辑按钮事件
            $(".btn-input").click(function () {
                //清空模态框信息
                $("#editForm input").val("");
                //获取自定义数据
                var json=$(this).data("json");
                //回显数据
                if (json) {

                    $("#editForm input[name='id']").val(json.id);
                    $("#editForm input[name='name']").val(json.name);
                    $("#parentName").val(json.parent.name);
                    $("#parentId").val(json.parent.id);
                }else{
                    var selected=$('#treeview').treeview('getSelected');
                    if(selected.length>0) {
                        $("#parentId").val(selected[0].id);
                        $("#parentName").val(selected[0].text);
                    }

                }
                //显示模态框
                $("#inputModal").modal("show");
            });
            //绑定保存按钮的事件
            $(".btn-submit").click(function () {
                $("#editForm").ajaxSubmit(function (data) {
                    reload();
                })
            });

            //绑定推荐按钮点击事件
            $(".btn-hot").click(function () {
                //获取自定义数据
                var json=$(this).data("json");
                var state=1;
                if(json.state) {
                    state = 0;
                }

                $.messager.confirm("温馨提示","亲，真的要设置推荐吗？",function () {
                    $.post("/region/updateState.do",{id:json.id,state:state},function (data) {
                        if(data.success) {
                            reload();
                        }
                    });
                })
            });
        });
    </script>
</head>
<body>
<table id="template" style="display: none">
    <tr>
        <td></td>
        <td></td>
       <td>
           <a class="btn btn-info btn-xs btn-input" >
               <span class="glyphicon glyphicon-pencil"></span>编辑
           </a>
           <a class="btn btn-danger btn-xs btn-hot" >
               <span class="glyphicon glyphicon-thumbs-up">设为推荐</span>
           </a>

       </td>
    </tr>
</table>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
            <#--设置作用于贡献数据，以回显菜单栏信息-->
            <#assign menu="region"/>
            <#include "../common/menu.ftl"/>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">地区管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/region/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <a  class="btn btn-success btn-input" role="button">
                    <span class="glyphicon glyphicon-plus"></span> 添加
                </a>
            </form>
                <div class="row">
                    <div class="col-sm-4">
                        <div id="treeview"></div>
                    </div>
                    <div class="col-sm-8">
                        <table class="table table-striped table-hover regionTable">
                            <thead>
                                <tr>
                                    <th>编号</th>
                                    <th>名称</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
        </div>
    </div>
</div>


<#--编辑模态框-->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/region/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" name="id">
                    <div class="form-group">
                        <label for="name" class="col-sm-4 control-label">名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="name" name="name"
                                   placeholder="请输入地区名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">上级地区：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="parentName" readonly>
                            <input type="hidden" class="form-control" id="parentId" name="parent.id" >
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary btn-submit">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</body>
</html>